package cn.hdu.goods.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "Filter",urlPatterns = {
        "/jsps/cart/*","/CartItemServlet","/jsps/order/*","/OrderServlet"})
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest req = (HttpServletRequest) request;
        Object user = req.getSession().getAttribute("sessionUser");
        if (user == null) {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "您还没有登录，不能访问本资源");
            req.getRequestDispatcher("/jsps/msg.jsp").forward(req, response);
        } else {
            chain.doFilter(request, response);
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
